package com.example.litepl;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import org.litepal.FluentQuery;
import org.litepal.LitePal;
import org.litepal.crud.LitePalSupport;
import org.litepal.crud.callback.FindMultiCallback;
import org.litepal.crud.callback.SaveCallback;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button add;
    private Button delete;
    private Button upData;
    private Button query;
    private EditText account;
    private EditText passWord;
    private TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        add = (Button) findViewById(R.id.add);
        delete = (Button) findViewById(R.id.delete);
        upData = (Button) findViewById(R.id.upData);
        query = (Button) findViewById(R.id.query);
        add.setOnClickListener(this);
        delete.setOnClickListener(this);
        upData.setOnClickListener(this);
        query.setOnClickListener(this);
        account = (EditText) findViewById(R.id.account);
        account.setOnClickListener(this);
        passWord = (EditText) findViewById(R.id.passWord);
        passWord.setOnClickListener(this);
        text = (TextView) findViewById(R.id.text);
        text.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        submit();
        String name = account.getText().toString();
        String password = passWord.getText().toString();
        switch (v.getId()) {
            case R.id.add:
                LitePalUser db = new LitePalUser();
                db.setName(name);
                db.setPassword(password);
                boolean save = db.save();
                Toast.makeText(MainActivity.this,save+"",Toast.LENGTH_SHORT).show();
//                List<LitePalUser> list = new ArrayList<>();
//                //一次性添加
//                for (int i = 0; i < 5; i++) {
//                    LitePalUser db = new LitePalUser();
//                    db.setName(name);
//                    db.setPassword(password);
//                    list.add(db);
//                }
//                LitePal.saveAll(list);
//                break;
            case R.id.delete:
//                //删除单个记录，id=1
//                LitePal.delete(LitePalUser.class, 1);
//                //删除数据库中NewsBean表的所有记录
//                LitePal.deleteAll(LitePalUser.class);
//                //删除数量大于100的
//                LitePal.deleteAll(LitePalUser.class, "id > ?", "100");
//                //删除标题为张三的
//                LitePal.deleteAll(LitePalUser.class, "name = ?", name);
                //删除标题为张三，内容为李四的记录
                int i = LitePal.deleteAll(LitePalUser.class, "name = ? and password = ?", name, password);
                Toast.makeText(MainActivity.this,i+"",Toast.LENGTH_SHORT).show();
                break;
            case R.id.upData:
//                //修改ID为1的记录，并将标题和内容分别设置为张三、李四
//                LitePalUser litePalUser = LitePal.find(LitePalUser.class, 1);
//                litePalUser.setName("小铁");
//                litePalUser.setPassword("123456");
//                boolean save1 = litePalUser.save();
//                Toast.makeText(MainActivity.this,save1+"",Toast.LENGTH_SHORT).show();
//                //如下方式同上
//                LitePalUser litePalUser1 = new LitePalUser();
//                litePalUser1.setName("小铁");
//                litePalUser1.setPassword("123456");
//                //直接更新id为1的记录
//                int update = litePalUser1.update(1);
//                Toast.makeText(MainActivity.this,update+"",Toast.LENGTH_SHORT).show();
//                //修改ID为1的记录
//                LitePalUser litePalUser2 = new LitePalUser();
//                //更新所有name为name的记录,更新为小铁
//                litePalUser2.setName("小铁");
//                litePalUser2.updateAll("name = ?", name);
                //将name为name，password为password的数据修改为小铁、123456
                LitePalUser litePalUser3 = new LitePalUser();
                litePalUser3.setName("小铁");
                litePalUser3.setPassword("123456");
                litePalUser3.updateAll("name = ? and password = ?", name, password);
                break;
            case R.id.query:
                //查询表中所有的记录，返回的是泛型为NewsBean的List集合
                List<LitePalUser> all = LitePal.findAll(LitePalUser.class);

                //查找表id为1的记录
                LitePalUser litePalUser = LitePal.find(LitePalUser.class, 1);

                //获取表中的第一条数据与最后一条数据
                LitePalUser first = LitePal.findFirst(LitePalUser.class);
                LitePalUser last = LitePal.findLast(LitePalUser.class);

                //查询表中的第5、10、15条数据
                List<LitePalUser> all1 = LitePal.findAll(LitePalUser.class, 5, 10, 15);
                //或者定义一个数组
                long[] ids = new long[]{5, 10, 15};
                List<LitePalUser> all2 = LitePal.findAll(LitePalUser.class, ids);
                //查找name为name的记录,并且以时长作排序(按时间desc倒序  asc 正序)，where()方法接收任意个字符串参数，
                //其中第一个参数用于进行条件约束，从第二个参数开始，都是用于替换第一个参数中的占位符的。那这个where()方法就对应了一条SQL语句中的where部分。
                List<LitePalUser> time_desc = LitePal.where("name = ?", name).order("time desc").find(LitePalUser.class);
                //将查询出的按照的时间倒序排列，只要name和password这两列数据，即最新发布的放在最前面，那就可以这样写：
                LitePal.select("name", "password")
                        .where("name > ?", name)
                        .order("time desc").find(LitePalUser.class);
                //设置查询的数量与偏移量
                List<LitePalUser> litePalUsers = LitePal.select("title", "content")
                        .where("name > ?", name)
                        .order("time desc")
                        .limit(30)//只查询前面30条
                        .offset(20)//分页查询，每次查20条
                        .find(LitePalUser.class);
                break;

    }
    }
    private void syncQuery() {
        //1、 异步查询示例
        //使用findallasync() 代替 findall() ，然后调用 listen() 方法，查询完成就会回调onFinish(）方法
        LitePal.findAllAsync(LitePalUser.class).listen(new FindMultiCallback<LitePalUser>() {
            @Override
            public void onFinish(List<LitePalUser> allSongs) {
                //查询结果
            }
        });
        //2、异步保存示例
        LitePalUser litePalUser1 = new LitePalUser();
        litePalUser1.setName("小铁");
        litePalUser1.setPassword("123456");
        litePalUser1.saveAsync().listen(new SaveCallback() {
            @Override
            public void onFinish(boolean success) {

            }
        });}
    private void submit() {
        // validate
        String accountString = account.getText().toString().trim();
        if (TextUtils.isEmpty(accountString)) {
            Toast.makeText(this, "账户", Toast.LENGTH_SHORT).show();
            return;
        }
        String passWordString = passWord.getText().toString().trim();
        if (TextUtils.isEmpty(passWordString)) {
            Toast.makeText(this, "密码", Toast.LENGTH_SHORT).show();
            return;
        }
        // TODO validate success, do something
    }
}
